package org.springblade.modules.system.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.modules.system.entity.Computer;
import org.springblade.modules.system.service.ComputerService;
import org.springblade.modules.system.utils.PageBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 接口文档测试路径http://localhost:8080/doc.html
 */
@Controller
@RequestMapping("/computer")
@Api("computer")
public class ComputerController {

	@Autowired
	private ComputerService computerService;

	@ResponseBody
	@ApiOperation(value = "查询",httpMethod = "post")
	@RequestMapping(value = "/openComputerTable", method = RequestMethod.POST)
	public HashMap<String, Object> openComputerTable(Computer computer, int pageSize, int currentPage) throws Exception {
		HashMap<String, Object> result = new HashMap<String, Object>();
		PageBounds<Computer> pageBounds = computerService.openComputerTable(computer, currentPage, pageSize);
		PageBounds<Computer> list = computerService.selectByName(computer);
		if (computer.getDname() != null && computer.getDname() != ""|| computer.getPid() != null
		) {
			result.put("data", list);
		} else {
			result.put("data", pageBounds);
		}
		return result;
	}

	/**
	 * 加载电脑类别信息
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllComputerType", method = RequestMethod.POST)
	public HashMap<String, Object> getAllComputerType(Computer computer) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		List<Computer> list = computerService.getAllComputerType(computer);
		result.put("data", list);
		return result;
	}

	/**
	 * 添加
	 */
	@ResponseBody
	@RequestMapping(value = "/addComputer", method = RequestMethod.POST)
	public HashMap<String, Object> addComputer(Computer computer) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		//设置当前时间
		// Date date = new Date();
		//computer.setCreateTime(date);
		computerService.addComputer(computer);
		return result;
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping(value = "/updateComputer", method = RequestMethod.POST)
	public HashMap<String, Object> updateComputer(Computer computer) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		computerService.updateComputer(computer);
		return result;
	}

	/**
	 * 通过id删除
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteComputerById", method = RequestMethod.POST)
	public HashMap<String, Object> deleteComputerById(Integer id) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		computerService.deleteComputerById(id);
		return result;
	}

	/**
	 * 批量删除
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteComputerByIds", method = RequestMethod.POST)
	public HashMap<String, Object> deleteComputerByIds(Integer[] ids) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		computerService.deleteComputerByIds(Arrays.asList(ids));
		return result;
	}

}
